package com.lagou.config;

import com.lagou.pojo.Configuration;
import com.lagou.pojo.MappedStatement;
import org.dom4j.Document;
import org.dom4j.DocumentException;
import org.dom4j.Element;
import org.dom4j.io.SAXReader;

import javax.xml.parsers.SAXParser;
import java.io.InputStream;
import java.util.List;

/**
 * 解析配置文件Mapper.xml，同时把结果放到Configuration对象中
 */
public class XMLMapperBuilder {

    private Configuration configuration;

    private static final String xpathExpression_select = "//select";
    private static final String xpathExpression_insert = "//insert";
    private static final String xpathExpression_update = "//update";
    private static final String xpathExpression_delete = "//delete";

    public XMLMapperBuilder(Configuration configuration){
        this.configuration = configuration;
    }

    public void parse1(InputStream inputStream) throws DocumentException {
        Document document = new SAXReader().read(inputStream);
        Element rootElement = document.getRootElement();

        String namespace = rootElement.attributeValue("namespace");
        List<Element> list = rootElement.selectNodes("//select");
        for(Element element : list){
            //取出select标签包含的信息，存放到MappedStatement对象中
            String id = element.attributeValue("id");
            String resultType = element.attributeValue("resultType");
            String parameterType = element.attributeValue("parameterType");
            String sqlText = element.getTextTrim();
            MappedStatement mappedStatement = new MappedStatement();
            mappedStatement.setId(id);
            mappedStatement.setResultType(resultType);
            mappedStatement.setParamterType(parameterType);
            mappedStatement.setSql(sqlText);

            //key就是命名空间+id
            String key = namespace+"."+id;
            configuration.getMappedStatementMap().put(key,mappedStatement);
        }
    }

    public void parse(InputStream inputStream) throws DocumentException {
        Document document = new SAXReader().read(inputStream);
        Element rootElement = document.getRootElement();

        String namespace = rootElement.attributeValue("namespace");
        List<Element> selectNodes = rootElement.selectNodes(xpathExpression_select);
        setConfiguration(selectNodes,namespace);
        List<Element> insertNodes = rootElement.selectNodes(xpathExpression_insert);
        setConfiguration(insertNodes,namespace);
        List<Element> updateNodes = rootElement.selectNodes(xpathExpression_update);
        setConfiguration(updateNodes,namespace);
        List<Element> deleteNodes = rootElement.selectNodes(xpathExpression_delete);
        setConfiguration(deleteNodes,namespace);

    }

    private void setConfiguration(List<Element> list,String namespace){
        for(Element element : list){
            //取出select标签包含的信息，存放到MappedStatement对象中
            String id = element.attributeValue("id");
            String resultType = element.attributeValue("resultType");
            String parameterType = element.attributeValue("parameterType");
            String sqlText = element.getTextTrim();
            MappedStatement mappedStatement = new MappedStatement();
            mappedStatement.setId(id);
            mappedStatement.setResultType(resultType);
            mappedStatement.setParamterType(parameterType);
            mappedStatement.setSql(sqlText);

            //key就是命名空间+id
            String key = namespace+"."+id;
            configuration.getMappedStatementMap().put(key,mappedStatement);
        }

    }





}
